from selenium import webdriver
import unittest
import time
import os
from selenium.common.exceptions import NoAlertPresentException
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.by import By


class Baidu1(unittest.TestCase):

    def setUp(self):
        self.driver = webdriver.Chrome()
        self.url = "https://www.baidu.com/"
        self.driver.maximize_window()
        time.sleep(3)
    def tearDown(self):
        self.driver.quit()
    def test_hao(self):
        driver = self.driver
        url = self.url
        driver.get(url)
        driver.find_element(By.LINK_TEXT, "hao123").click()
        time.sleep(6)
    def test_hbaidu(self):
        driver = self.driver
        url = self.url
        driver.get(url)
        driver.find_element(By.ID, "kw").send_keys("突如其来的假期")
        driver.find_element(By.ID, "su").submit()
        time.sleep(5)
        print(driver.title)
        time.sleep(6)
    def saveScreenAsPhoto(self, driver, file_name):
        if not os.path.exists("./image"):
            os.makedirs("./image")
        now = time.strftime("%Y%m%d-%H%M%S", time.localtime(time.time()))
        driver.get_screenshot_as_file("./image/" + now + "-" + file_name)
        time.sleep(3)
    if __name__ == "__main__":
        unittest.main()